<template>
  <div>
    <el-card class="box-card">
      <!-- card里面的封装的组件左按钮，右自定义-->
      <pageToolLys>
        <template v-slot:before>
          <el-button size="small" type="primary" @click="addCustomerInfo">添加客户</el-button>
        </template>
        <template v-slot:after>
          <el-row type="flex" class="search">
            <el-select v-model="selectAgent">
              <el-option label="全部经纪" :value="null" />
              <el-option v-for="item in agentList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
            <el-input v-model="filterInput" class="inp" placeholder="请输入关键字" />
            <el-button class="btn" @click="filterAgent">筛选</el-button>
          </el-row>
        </template>
      </pageToolLys>
      <!-- 表格 -->
      <el-table border :data="tableData" stripe style="width: 100%">
        <el-table-column prop="image" label="图片" width="150px">
          <template v-slot="{ row }">
            <img :src="row.image" alt="" style="width:150px; padding-right: 20px;">
          </template>
        </el-table-column>
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column prop="phone" label="电话" />
        <el-table-column prop="agent.name" label="经纪" />
        <el-table-column prop="wechat" label="微信" />
        <el-table-column prop="desc" label="介绍" width="150px" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button type="primary" size="small" @click="edit(row.id)">编辑</el-button>
            <el-button type="danger" size="small" @click="delect(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row type="flex" justify="center">
        <el-pagination layout="prev, pager, next" :total="total" :page-size="pageSize" @current-change="pageChange" />
      </el-row>

    </el-card>
    <!-- 弹框dialog编辑新增客户 -->
    <EditCustomer
      ref="EditCustomer"
      :dialog-visible.sync="showDialog"
      :agent-list="agentList"
      @updataList="getCustomerList"
      @updataNum="getCustomerNum"
    />
  </div>
</template>
<script>
import EditCustomer from './components/EditCustomer.vue'
import { getCustomerList, getAgentsList, deleteCustomer, customerNum } from '@/api/customer'
export default {
  components: {
    EditCustomer
  },
  data() {
    return {
      tableData: [],
      showDialog: false,
      agentList: [],
      page: 0, // 第几个开始请求数据数量
      pageSize: 2, // 每个分页多少条数据
      total: 0, // 数据总数量
      selectAgent: null, // 选择经纪id
      filterInput: null // 双向绑定input
    }
  },
  async created() {
    this.getCustomerList()
    // 获取客户数量
    this.getCustomerNum()
    this.getAgentsList()
  },
  methods: {
    async getCustomerNum() { // 获取客户总数量
      const res = await customerNum({
        name_contains: this.filterInput,
        agent: this.selectAgent
      })
      // console.log(1, res)
      this.total = res.data
    },
    async getCustomerList() { // 刷新客户列表数据
      const res = await getCustomerList({
        _limit: this.pageSize,
        _start: this.page,
        name_contains: this.filterInput,
        agent: this.selectAgent
      })
      // console.log(79, res)
      this.tableData = res.data
      // console.log(81,this.tableData.length)
    },
    async edit(id) { // 修改按钮
      const res = await getCustomerList({
        id
      })
      // console.log(res)
      this.showDialog = true
      this.$refs.EditCustomer.formData = res.data[0]
      this.$refs.EditCustomer.imageUrl = res.data[0].image
    },
    async getAgentsList() { // 获取经纪列表
      const res = await getAgentsList()
      this.agentList = res.data
    },
    async addCustomerInfo() { // 添加客户按钮
      this.showDialog = true
    },
    delect(id) { // 删除角色
      this.$confirm('是否删除该客户', '提示', {
        type: 'warning'
      }).then(async() => {
        if (this.tableData.length === 1 && this.page !== 0) {
          this.page = this.page - this.pageSize
        }
        await deleteCustomer(id)
        this.getCustomerList() // 请求列表
        this.getCustomerNum() // 请求数量
      }).catch(err => {
        console.log('你点击了取消', err)
      })
    },
    pageChange(page) { // 分页切换发送请求
      this.page = (page - 1) * this.pageSize
      this.getCustomerList()
      this.getCustomerNum()
    },
    filterAgent() {
      console.log(this.selectAgent)
      console.log(this.filterInput)
      this.getCustomerList()
      this.getCustomerNum()
    }
  }
}

</script>
<style lang="scss" scoped>
::v-deep .el-table__body-wrapper .el-table_1_column_1 {
  .cell {
    width: 150px;
    height: 150px;
    padding-right: 20px;
  }
}
</style>
